package com.example.androidapp2;

import java.util.List;

import com.example.androidapp2.DbClasses.ItemCategoria;
import com.example.androidapp2.DbClasses.ItemIngresos;
import com.example.androidapp2.DbClasses.Utils;
import com.xyzdesa.droidnomy.R;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class PopUpConfigu extends Dialog  {
	DbAdapter DbConect;
	Context Ctx;

	public EditText txtSueldo;	
	public Button btnGuardar;
	public Button btnEliminar;
	public CheckBox chbAutomatico;
	public Spinner spnPeriodos;
	public Spinner spnIngresos;
	public Spinner spnCate;	
	private Boolean Checked;

	public PopUpConfigu(Context context, DbAdapter db) {
		super(context);
		Ctx = context;
		DbConect = db;

		this.setContentView(R.layout.pup_config);		

		txtSueldo = (EditText) findViewById(R.id.txtSueldo);		
		btnGuardar = (Button) findViewById(R.id.btnGuardar);
		btnEliminar = (Button) findViewById(R.id.btnEliminar); 
		chbAutomatico = (CheckBox) findViewById(R.id.chbAutomatico);
		spnPeriodos = (Spinner) findViewById(R.id.spnPeriodos);
		spnIngresos = (Spinner) findViewById(R.id.spnIngresos);		
		spnCate = (Spinner) findViewById(R.id.spnCategorias);
		SetListenerBotonesPopUpTarjetas();
	}

	public void SetListenerBotonesPopUpTarjetas()
	{	
		btnGuardar.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				if(txtSueldo.getText().toString().length()>0){
					if(spnIngresos.getVisibility() == View.INVISIBLE){						
						DbConect.InsertMovimiento(Integer.parseInt(txtSueldo.getText().toString()), "In", Utils.GetFecha(), "EFT", 0);
						if(chbAutomatico.isChecked())
							DbConect.SetSueldoAutomatico(txtSueldo.getText().toString());
					}else
					{
						if(((ItemIngresos)spnIngresos.getSelectedItem()).Id == 0){					
							
							DbConect.InsertMovimiento(Integer.parseInt(txtSueldo.getText().toString()), "In", Utils.GetFecha(), "EFT", 0);
							if(chbAutomatico.isChecked())
								DbConect.SetSueldoAutomatico(txtSueldo.getText().toString());
						}
						else{							
							int cod = (spnCate.getVisibility() == View.VISIBLE)?((ItemCategoria)spnCate.getSelectedItem()).Codigo:0;
							if(cod == 0 && chbAutomatico.isChecked() && !Checked)
								DbConect.SetSueldoAutomatico(txtSueldo.getText().toString());
							else if(cod == 0 && !chbAutomatico.isChecked() && Checked)
								DbConect.ClearSueldoAutomatico();
							
							DbConect.UpdateMovimientos(((ItemIngresos)spnIngresos.getSelectedItem()).Id, Integer.parseInt(txtSueldo.getText().toString()),cod);
						}
					}
					txtSueldo.setText("");
					((MainActivity)Ctx).ActualizarProgressBar();
					hide();
				}
			}
		});

		btnEliminar.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				if(((ItemIngresos)spnIngresos.getSelectedItem()).Id != 0){		
					((MainActivity)Ctx).pupConfirma.show("EliminarMov", new int[]{((ItemIngresos)spnIngresos.getSelectedItem()).Id});					
					txtSueldo.setText("");				
					hide();
				}
			}
		});
	}

	public void show(boolean esIngreso)
	{
		Checked=false;
		this.setTitle(R.string.pupModifica);

		spnPeriodos.setVisibility(View.VISIBLE);
		spnIngresos.setVisibility(View.VISIBLE);		
		chbAutomatico.setVisibility(View.VISIBLE);
		
		SetSpnPeriodos();

		this.show();
	}

	public void Show()
	{
		Checked=false;
		this.setTitle(R.string.pupConfig);		
		spnPeriodos.setVisibility(View.INVISIBLE);
		spnIngresos.setVisibility(View.INVISIBLE);
		spnCate.setVisibility(View.INVISIBLE);
		btnEliminar.setVisibility(View.INVISIBLE);		
		chbAutomatico.setVisibility(View.VISIBLE);

		this.show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode ==  KeyEvent.KEYCODE_BACK && spnIngresos.getVisibility() == View.VISIBLE){
			txtSueldo.setText("");
			this.hide();
		}
		return true;
	}

	public void SetSpnIngresos(List<ItemIngresos> ls)
	{
		ArrayAdapter<ItemIngresos> spinnerAdapter = new ArrayAdapter<ItemIngresos>(this.Ctx,android.R.layout.simple_spinner_item,ls);
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
		spnIngresos.setAdapter(spinnerAdapter);

		spnIngresos.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				spnCate.setVisibility(View.INVISIBLE);
				if(((ItemIngresos)spnIngresos.getSelectedItem()).Id != 0){
					chbAutomatico.setVisibility(View.INVISIBLE);
					btnEliminar.setVisibility(View.VISIBLE);
					txtSueldo.setText(String.valueOf(((ItemIngresos)spnIngresos.getSelectedItem()).Valor));
					if(((ItemIngresos)spnIngresos.getSelectedItem()).Categoria != null){
						spnCate.setVisibility(View.VISIBLE);
						List<ItemCategoria> ls = DbConect.GetCategorias("spnCategorias",true,false);
						ArrayAdapter<ItemCategoria> spnAdapter = new ArrayAdapter<ItemCategoria>(Ctx,android.R.layout.simple_spinner_item, ls);
						spnAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
						spnCate.setAdapter(spnAdapter);

						for (int i = 0; i < ls.size(); i++) {
							if(ls.get(i).Descripcion.equals(((ItemIngresos)spnIngresos.getSelectedItem()).Categoria)){
								spnCate.setSelection(i, true);
								break;
							}
						}
					}else{
						chbAutomatico.setVisibility(View.VISIBLE);
						int sueldo = DbConect.GetIngresoAutomatico();
						if(Integer.parseInt(txtSueldo.getText().toString()) == sueldo){
							chbAutomatico.setChecked(true);
							Checked = true;
						}
						else{ 
							chbAutomatico.setChecked(false);
							Checked = false;
						}
					}					
				}else{ 
					chbAutomatico.setChecked(false);
					btnEliminar.setVisibility(View.INVISIBLE);
					Checked = false;
				}

			}

			public void onNothingSelected(AdapterView<?> arg0) {}

		});
	}	

	public void SetSpnPeriodos()
	{
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this.Ctx,android.R.layout.simple_spinner_item,DbConect.GetFechasSpinner());
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
		spnPeriodos.setAdapter(spinnerAdapter);

		spnPeriodos.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) 
			{
				Resources res = Ctx.getResources();				
				if(arg0.getSelectedItem().toString().equals(res.getString(R.string.spnItemCate)))
					SetSpnIngresos(DbConect.GetIngresos());
				else if(arg0.getSelectedItem().toString().length()==4)
					SetSpnIngresos(DbConect.GetIngresosAnio(arg0.getSelectedItem().toString()));
				else
					SetSpnIngresos(DbConect.GetIngresosMes(arg0.getSelectedItem().toString().replace(" - ", "")));				
			}

			public void onNothingSelected(AdapterView<?> arg0) {}

		});
	}	
}
